home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0024_WORD2HEX.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  9KB  |  193 lines

  1. {
  2. > How does the following Function make a Word into Hex:
  3.  
  4.  - Dissection:
  5. }
  6.  
  7. Type
  8.   Str4 : String[4];
  9.  
  10. Function WordtoHex(W : Word) : St4
  11. Var
  12.   HexStr : St4;
  13.  
  14.   Function Translate(B : Byte) : Char;
  15.  
  16.   { This Function takes a number from 0 to 15 and makes it into a hex digit.}
  17.  
  18.   begin
  19.     if B < 10 then
  20.     { if it's 0..9 }
  21.       Translate := Chr(B + 48)
  22.   { These statements use math on Characters... ascii 48 is '0'.
  23.     Could have been written: Translate := Chr(B + ord('0')) }
  24.     else
  25.       Translate := Chr(B + 55);
  26.   { This one is For letters A~F. ascii 55 isn't anything, but if you add
  27.     $A (10) to 55 you get 65, which is the ascii code For 'A'
  28.     This could have been written: Translate := Chr(B + ord('A')-$A); }
  29.   end;
  30.  
  31. begin
  32.   HexStr := ' ';
  33.   HexStr := HexStr + Translate(Hi(W) shr 4);
  34.   { Hi(W) takes the high Byte of Word W.
  35.     shr 4 means the same as divide by 16...
  36.     What they're Really doing here is taking each nibble of the hex Word
  37.     and isolating it, translating it to hex, and adding it to the String. }
  38.   HexStr := HexStr + Translate(Hi(W) and 15);
  39.   HexStr := HexStr + Translate(Lo(W) shr 4);
  40.   HexStr := HexStr + Translate(Lo(W) and 15);
  41.   WordtoHex := HexStr;
  42. end;
  43. {
  44. > I am learning Pascal and don't understand something.  How
  45. > does the following Function make a Word into Hex:
  46.  
  47. It doesn't, at least not as present! But if you changes two things, maybe
  48. spelling-errors, it will work. This is a bit hard to explain and grasp, because
  49. it involves operations at a less abstract level than the one that you usually
  50. work on in TP. Remember, when a number is stored in memory, it's stored binary,
  51. hexadecimal notion is just For making it easier For man to read. I don't know
  52. if you know how to Write and read binary- and hexadecimal-numbers, in Case you
  53. don't know it's all here...
  54.  
  55. On PC, a Word, in the range 0 to 65535, has 16 bits. A Word written in binary
  56. notion For this reason contains 16 digits, 0's or 1's! But a Word written in
  57. hexadecimal notion contains 4 digits. Simple math tells us that one digit in
  58. hex-notion is equal to four digits binary. Four digits binary gives 16
  59. combinations (2^4). ThereFore, each hexadecimal digit must be able to contain
  60. values from decimal 0-decimal 15, _in one digit_! Our normal digits, 0-9, isn't
  61. sufficient For this, we must use 6 other digits. The designers of this system
  62. choosed A-F as the extra digits. This means, in hex the digits are 0, 1, 2, 3,
  63. 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. Hanging on?
  64.  
  65. >    Function WordtoHex(W : Word) : St4
  66.  
  67. Compile-time error: You must have a semicolon after the Function header-line.
  68.  
  69. >    Var
  70. >        HexStr : St4;
  71.  
  72. >        Function Translate(B : Byte) : Char;
  73. >        begin
  74. >           if B < 10
  75. >               then
  76. >                   Translate := Chr(B + 48)
  77. >               else
  78. >                   Translate := Chr(B + 55);
  79. >        end;
  80.  
  81. This is clearer as:
  82.  
  83.   if b < 10
  84.     then Translate := Chr(b+ord('0'))
  85.     else Translate := Chr(b+ord('A')-10);
  86.  
  87. Think about the first Case, when b < 10, if b were 0, the expression would be
  88. '0' plus 0, '0'!. if b were 1, it's '0' plus 1, '1'!. This works because in the
  89. ASCII-table the numbers are sequential ordered. But '0' plus 10 would be ':',
  90. because it happens to be after the numbers.
  91.  
  92. then, when we want 'A'-'F, we would need to start from 'A'. But we can't add 10
  93. to 'A' For getting 'A' and 11 For getting 'B' and that like. First we must make
  94. the value relative 'A'. Because the values that we're working on here is in the
  95. range 10 to 15, we can decrease it With 10 and get 0 to 5. then is OK to use
  96. them relative 'A'. As beFore, 'A' plus 0 is 'A', 'A' plus 1 is 'B', and so on.
  97.  
  98. However, this routine has no safety check, it will gladly return 'G' For 16,
  99. because 'A'+6 is 'G'. It doesn't care if the value is within hexadecimal range
  100. or not (numbers bigger than 15 can't be turned into one hex digit, they need
  101. more digits). But here it's OK, because the routine is local to WordtoHex that
  102. will never pass anything else than 0 to 15.
  103.  
  104. >    begin
  105. >        HexStr := ' ';
  106.  
  107. Logical error: You must initalize HexStr to an empty String, '', if not it will
  108. consist of a space and three hex digits, not four. A hex-Word String is
  109. Composed of four hexadeciamal-digits. Because you have declared the String as a
  110. Variable of the Type St4 and St4 only allows four Chars, exactly what is needed
  111. For a hexWord-String, the last one added will be discarded if you have a space
  112. at the beginning, filling up one position.
  113.  
  114. >        HexStr := HexStr + Translate(Hi(W) shr 4);
  115. >        HexStr := HexStr + Translate(Hi(W) and 15);
  116. >        HexStr := HexStr + Translate(Lo(W) shr 4);
  117. >        HexStr := HexStr + Translate(Lo(W) and 15);
  118. >        WordtoHex := HexStr;
  119. >    end;
  120.  
  121. It would be easier to read if the 'and'-value were in hex-notation, $000F. See
  122. below For explanation why. However, this part requires some understanding of
  123. the bits. It's probably best show With an example. Let's say, our number W is
  124. $1234.
  125.  
  126. $1234 is written 0001 0010 0011 0100 in binary. Each hex-digit corresponds to a
  127. four-group binary digits.
  128.  
  129. ■) The binary number 0001 is 0*(2^3) + 0*(2^2) + 0*(2^1) + 1*(2^0). It gives
  130. 0+0+0+1=1 in decimal.
  131.  
  132. ■) The binary number 0101 is 0*(2^3) + 1*(2^2) + 0*(2^1) + 1*(2^0). It gives
  133. 0+4+0+1=5 in decimal.
  134.  
  135. ■ The _decimal_ number 1101 is 1*(10^3) + 1*(10^2) + 0*(10^1) + 1*(10^0). It
  136. gives 1000+100+0+1=1011! As you can see, the only difference between the
  137. decimal and the binary and the hexadecimal system is the base-power. True, the
  138. hex-system Uses strange digits For us used to decimal, but For the ones used to
  139. binary, 2-9 is equally strange...
  140.  
  141. Like our decimal system, in hex and binary, it's unnescessary to include
  142. leading zeros, i. e. $0001 = $1 (of course you can't remove trailing zeroes,
  143. decimal 1000 certainly isn't equal to decimal 1...). But you will note that I
  144. sometimes include these leading zeroes, just because it looks good (?). and
  145. writing binary number 1000 0000 is like writing 10000 in decimal as 10,000;
  146. it's only For easy reading, but the Compiler won't allow it.
  147.  
  148. However, I hope you grasp a least something of my extremly bad explanation :-(,
  149. or maybe you know it beFore?! Now, let's look at the things that happens when
  150. the above statements are executed and w = $1234 (0001 0010 0011 0100).
  151.  
  152. Hi returns the upper 8 bits of the Word, in this Case 0001 0010; Lo returns the
  153. lower bits (!), 0011 0100. The above code Uses 'and' and 'shr', a breif
  154. explanation of them will probably be nescessary (oh no :-)).
  155.  
  156. ■ and, when not used as a Boolean operator, Uses two binary numbers and, For
  157. each bit, tests them. if _both_ bits are set (equal to 1) the resuling bit is
  158. set to 1, if any or both of them is cleared (equal to 0) the result is 0. This
  159. means:
  160.  
  161.  
  162.   0001 0010   Hi(w)                     0011 0100   Lo(w)
  163.   0000 1111   and With 15 or $000F      0000 1111   and With 15 or $000F
  164.   ---------                             ---------
  165.   0000 0010   0010 binary = 2 hex       0000 0100   0100 binary = 4 hex
  166.  
  167. This was the second and first statement, and out you get the second and first
  168. number! When we pass them to Translate, we get back '2' and '4'.
  169.  
  170. ■ shr, only For binary operations, shifts the bits to the right. The bits that
  171. passes over the right side is lost, and the ones that move on left side is
  172. replaced by zeroes. The bits shifts as many times as the value after the
  173. shr-keyWord, here 4 times. Like this:
  174.  
  175.   00010010   Hi(w)                     00110100   Lo(w)
  176.   --------             shr 4           --------
  177.   00001001        after one shift      00011010
  178.   00000100        after two shifts     00001101
  179.   00000010       after three shifts    00000110
  180.   00000001       after four shifts     00000011
  181.  
  182. Now we got binary 0001 and binary 0011, in hex $1 and $3. The first and third
  183. statement, and the first and third number! The String to return is digit1 +
  184. digit2 + digit3 + digit4, exactly what we want.
  185.  
  186. Hmm... Now I haven't told you anything about the binary or, xor, not and
  187. shl-keyWords... But I think this message is quiet long as it is, without that.
  188. But if you want more info or a better explanation, only drop me a msg here.
  189.  
  190. Happy hacking /Jake 930225 17.35 (started writing last night)
  191. PS. There may be some errors, I haven't proof-read the Text or my math. then,
  192. please correct me, anybody.
  193. }